package com.sm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.entity.News;
import com.sm.entity.ResultMap;
import com.sm.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/news")
public class NewsController {
    @Autowired
    ResultMap resultMap;
    @Autowired
    NewsService newsService;

    /**
     * @return 返回news视图
     * @throws Exception 所有异常
     */
    @RequestMapping("/news")
    public String news()throws Exception{
        return "admin/news/news";
    }

    /**
     * @return 返回最新新闻
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping(value = {"/news.do"})
    public ResultMap newsSearchDo()throws Exception{
        List<News> newsList = newsService.findAllNews();
        resultMap.getResultList().put("newsList",newsList);
        return resultMap;
    }

    /**
     * @return 返回最新新闻分页
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping(value = {"/newsearch"})
    public ResultMap newsSearchDoPH(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "5") Integer limit)throws Exception{
        PageHelper.startPage(curr,limit);
        List<News> newsList = newsService.findAllNews();
        PageInfo<News> pageInfo = new PageInfo<News>(newsList);
        resultMap.getResultList().put("page",pageInfo);
        return resultMap;
    }

    /**
     * @return 返回添加视图
     * @throws Exception 所有异常
     */
    @RequestMapping("/newsadd")
    public String newsAdd()throws Exception{
        return "admin/news/newsadd";
    }

    /**
     * 保存新闻
     * @return 返回添加成功
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("/newsadd.do")
    public ResultMap newsAddDo(News news)throws Exception{
        newsService.addNews(news);
        resultMap.setMessage("添加成功");
        return resultMap;
    }

    /**
     * @return 返回所有新闻
     * @throws Exception 所有异常
     */
    @RequestMapping("/newsadmin")
    public String newsAdmin()throws Exception{
        return "admin/news/newsadmin";
    }

    /**
     * @return 返回修改视图
     * @throws Exception 所有异常
     */
    @RequestMapping("/newsmodify/{id}")
    public String newsModify(@PathVariable("id")Integer nid,Model model)throws Exception{
        News news = newsService.findNewsByNid(nid);
        model.addAttribute("news",news);
        System.out.println(news);
        return "admin/news/newsmodify";
    }

    /**
     * @return 返回添加成功
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("/newsmodify.do")
    public ResultMap newsModifyDo()throws Exception{
//        newsService.addNews(news);
        return resultMap;
    }

    /**
     * @param nid 要删除新闻的ID
     * @return 返回提示信息
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("/newsdelete/{id}")
    public ResultMap newsDelete(@PathVariable("id")Integer nid)throws Exception{
        newsService.deleteNews(nid);
        return resultMap;
    }

    /**
     * @param nid 要修改的新闻id
     * @return 返回新闻
     * @throws Exception 所有异常
     */
    @ResponseBody
    @RequestMapping("/newsone/{id}")
    public ResultMap newsSearch(@PathVariable("id")Integer nid)throws Exception{
        News news = newsService.findNewsByNid(nid);
        resultMap.getResultList().put("news",news);
        return resultMap;
    }

    /**
     * @param nid 要查看新闻的ID
     * @param model model
     * @return 返回视图 服务器端渲染
     * @throws Exception 所有异常
     */
    @RequestMapping("/newsdetail/{id}")
    public String newsDetail(@PathVariable("id") Integer nid, Model model)throws Exception{
        News news = newsService.findNewsByNid(nid);
        model.addAttribute("news",news);
        return "admin/news/newsdetail";
    }
}
